package framework.sepal4java.webserver;

import framework.sepal4java.common.struct.SensorData;
import framework.sepal4java.lib4app.AbstractApp;
import framework.sepal4java.lib4app.AppRemoteConnector;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class JSPServletContextListener implements ServletContextListener {
    AppRemoteConnector conn = null;
    AbstractApp app = null;
    @Override
    public void contextInitialized(ServletContextEvent event) {
        app = new AbstractApp() {
            @Override
            public void getMsg(String sensorName, SensorData value) {

            }

            @Override
            public void configApp() {
                this.appName = "WebServer";
                this.appDescription = "This is WebServer";
            }
        };
        conn = AppRemoteConnector.getInstance();
        System.out.println("WebServer: " + conn);
        conn.connectPlatform("127.0.0.1", 9090);
        conn.registerApp(app);
        ServletContext context = event.getServletContext();
        context.setAttribute("conn", conn);
        HTMLMaker maker = HTMLMaker.getInstance();
        context.setAttribute("maker", maker);
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // 在Web应用程序停止时执行的代码
        if (conn != null && app != null) {
            conn.unregisterApp(app);
            conn.disConnectPlatform();
        }
    }
}